package com.gpay.erp.services.storage.impl;

import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.services.common.SystemCommonService;
import com.gpay.erp.services.purchase.ContractService;
import com.gpay.erp.services.purchase.ContractSubService;
import com.gpay.erp.services.purchase.bean.Contract;
import com.gpay.erp.services.purchase.bean.ContractSub;
import com.gpay.erp.services.storage.MandanService;
import com.gpay.erp.services.storage.StorageService;
import com.gpay.erp.services.storage.StorageSubService;
import com.gpay.erp.services.storage.bean.Mandan;
import com.gpay.erp.services.storage.bean.Storage;
import com.gpay.erp.services.storage.bean.StorageSub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wuzonghui on 15/11/23.
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Resource
    private BaseDao dao;
    @Autowired
    private SystemCommonService systemCommonService;
    @Autowired
    private StorageSubService storageSubService;
    @Autowired
    private MandanService mandanService;

    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractSubService contractSubService;



    @Override
    public int insert(Storage storage) {
        return dao.insert("storage.insert",storage);
    }

    @Override
    public int delete(Storage storage) {
        return 0;
    }

    @Override
    public int deletes(String[] ids) {
        return 0;
    }

    @Override
    public int update(Storage storage) {
        return dao.update("storage.update",storage);
    }

    @Override
    public Storage selectOne(Storage storage) {
        return null;
    }

    @Override
    public Storage selectById(String id) {
        return (Storage) dao.selectOne("storage.selectById",id);
    }


    @Override
    public Storage selectByFId(String id) {
        return (Storage) dao.selectOne("storage.selectByFId",id);
    }

    @Override
    public PagerModel selectPageList(Storage storage) {
        return dao.selectPageList("storage.selectPageList","storage.selectPageCount",storage);
    }

    @Override
    public List<Storage> selectList(Storage storage) {
        return null;
    }

    /**
     * 获取入库单号
     * @return
     */
    @Override
    public String getStorageCode() {
        return systemCommonService.saveTwoSelectByName("WI004");
    }

    /**
     * 入库明细对数据库表的操作
     * @param storage  库存表
     * @param storageSubs 库存明细表
     * @param contractSub 合同子项表
     * @return 修改库存表id
     */
    @Override
    public int saveStorageSubs(Storage storage, List<StorageSub> storageSubs, ContractSub contractSub){
        int insertId = 1;
        insertId = insert(storage); //入库表

        for (StorageSub storageSub:storageSubs) { //入库明细表
            storageSub.setF_storageId(String.valueOf(insertId));
            int insertSubId = storageSubService.insert(storageSub);
            Mandan mandan = getMandanInfor(storageSub);
            mandan.setF_storageSubId(String.valueOf(insertSubId));
            mandanService.insert(mandan);
        }

        int updateCid = contractSubService.update(contractSub); //修改合同

        contractSub.setStatusSub("2"); //查询收货完成状态
        List<ContractSub> contractSubs = contractSubService.selectByFidAndStatus(contractSub);
        if (contractSubs.size()==0) {
            Contract contract = contractService.selectById(contractSub.getF_id());
            contract.setContractStatus("2");
            contractService.update(contract);//更新采购合同收货状态
        }
        return insertId;
    }


    /**
     * 入库信息转换码单表
     *
     * @param storageSub 库存明细表
     * @return
     */
    public Mandan getMandanInfor(StorageSub storageSub) {
            Mandan mandan = new Mandan();
            mandan.setContractCode(storageSub.getContractCode());
            mandan.setContractSubCode(storageSub.getContractSubCode());
            mandan.setContractSubStatus(storageSub.getContractSubStatus());
            mandan.setStorageCode(storageSub.getStorageCode());
            mandan.setStorageSubCode(storageSub.getStorageSubCode());
            mandan.setStorageStatus(storageSub.getStorageStatus());
            mandan.setResourceNumber(storageSub.getResourceNumber());
            mandan.setPackageType(storageSub.getPackageType());
            mandan.setPackageNumber(storageSub.getPackageNumber());
            mandan.setStoragePage(storageSub.getStoragePage());
            mandan.setGoodsName(storageSub.getGoodsName());
            mandan.setGoodsCode(storageSub.getGoodsCode());
            mandan.setCpspecification(storageSub.getCpspecification());
            mandan.setMarks(storageSub.getMarks());
            mandan.setCdCode(storageSub.getCdCode());
            mandan.setCdName(storageSub.getCdName());
            mandan.setWarehouseName(storageSub.getWarehouseName());
            mandan.setWarehouseCode(storageSub.getWarehouseCode());
            mandan.setStandards(storageSub.getStandards());
            mandan.setQualityGrade(storageSub.getQualityGrade());
            //码单特有的销售信息
            mandan.setSaleNumber(storageSub.getStorageNo());
            mandan.setPutNumber(0.0);
            mandan.setListNumber(0.0);
            mandan.setLockNumber(0.0);
            mandan.setRealNumber(0.0);
            mandan.setPutPrice(0.0);
            mandan.setListPrice(0.0);
            mandan.setPutFlag("0");
            mandan.setListFlag("0");

            mandan.setStorageNo(storageSub.getStorageNo());
            mandan.setPurchasePrice(storageSub.getPurchasePrice());
            mandan.setStorageDate(storageSub.getStorageDate());
            mandan.setFurnaceNo(storageSub.getFurnaceNo());
            mandan.setPosation(storageSub.getPosation());
            mandan.setMdThick(storageSub.getMdThick());
            mandan.setMdWidths(storageSub.getMdWidths());
            mandan.setMdLong(storageSub.getMdLong());
            mandan.setCpspecification4(storageSub.getCpspecification4());
            mandan.setCpspecification5(storageSub.getCpspecification5());
            mandan.setCpspecification6(storageSub.getCpspecification6());
            mandan.setCpspecification7(storageSub.getCpspecification7());
            mandan.setCpspecification8(storageSub.getCpspecification8());
            mandan.setCpspecification9(storageSub.getCpspecification9());
            mandan.setCpspecification10(storageSub.getCpspecification10());
            mandan.setCargoUnits(storageSub.getCargoUnits());
            mandan.setMeteringMethod(storageSub.getMeteringMethod());
            mandan.setRemark(storageSub.getRemark());
            mandan.setAccountName(storageSub.getAccountName());
            mandan.setF_account(storageSub.getF_account());
            return mandan;
    }
    /**
     * 撤销入库单 对应表数据库操作
     * @param storage 库存表
     * @param storageSubs 库存子项表
     * @param contractSub 合同子项表
     * @param contract 合同主表
     * @return 修改库存表id
     */
    @Override
    public int deleteStorageSubList(Storage storage, List<StorageSub> storageSubs, ContractSub contractSub, Contract contract) {
        StorageSub storageSub = null;
        for (StorageSub storageSub1:storageSubs) {
            storageSub = storageSub1;
            storageSubService.update(storageSub1);
        }
        Double storagePage = 0.0;
        Double storageNo = 0.0;
        storageSub.setStorageSubStatus("0"); //查询子项全部撤销sum所有直
        HashMap hashMap = storageSubService.selectTotalByOne(storageSub);
        if(hashMap !=null) {
            if (hashMap.get("STORAGEPAGE") != null) {
                storagePage = (Double.parseDouble(hashMap.get("STORAGEPAGE").toString()));
            }
            if (hashMap.get("STORAGENO") != null) {
                storageNo = (Double.parseDouble(hashMap.get("STORAGENO").toString()));
            }

            storage.setStorageStatus("0");

        }

        storage.setStorageWeight(storageNo);
        storage.setStoragePage(storagePage);
        int updateId = update(storage);
        Mandan mandan = new Mandan();
        mandan.setF_storageId(storage.getId());
        int deleteId = mandanService.delete(mandan);

        contractSubService.update(contractSub);

        contractService.update(contract);

        return updateId;
    }

    /**
     * 调整入库单 对应数据库表操作
     * @param storage 调整入库主表信息 件数 入库量
     * @param storageSubs 调整入库子表信息 修改码单表对应信息 件数,入库量,可销量
     * @param contractSub 调整合同子表信息 修改可入库量
     * @return
     */
    @Override
    public int updateStorageSubList(Storage storage, List<StorageSub> storageSubs, ContractSub contractSub) {
        int updateId = update(storage);
        for (StorageSub storageSub:storageSubs) {
            storageSubService.update(storageSub);
            Mandan mandan = mandanService.selectByFstorageSubId(storageSub.getId());
            mandan.setStoragePage(storageSub.getStoragePage());
            mandan.setStorageNo(storageSub.getStorageNo());
            mandan.setSaleNumber(storageSub.getStorageNo());
            mandanService.update(mandan);
        }
        contractSubService.update(contractSub);
        return updateId;
    }

    /**
     * 撤销入库明细表子项,修改入库单主表入库量  修改采购合同入库量
     * @param storageSubId 撤销子项id
     * @return
     */
    @Override
    public int deleteCancelStorageSubList(String[] storageSubId) {
        int updateSubId = 1;
        StorageSub storageSub = null;
        String storageId ="";
        Double storagePage = 0.0;
        Double storageNo = 0.0;
        for (String subId:storageSubId) {
            storageSub = storageSubService.selectById(subId);
            if ("1".equals(storageSub.getStorageSubStatus())) {
                storageSub.setStorageSubStatus("0");
                storageSub.setContractSubStatus("1");
            }
            storageSubService.update(storageSub);
            storageId = storageSub.getF_storageId();
        }
        Storage storage = selectById(storageId);
        ContractSub contractSub = contractSubService.selectById(storage.getF_contractSubId());
        contractSub.setStatusSub("1"); //设置合同子项执行
        storageSub.setStorageSubStatus("1"); //查询子项是否全部撤销
        HashMap hashMap = storageSubService.selectTotalByOne(storageSub);
        if(hashMap !=null) {
            if (hashMap.get("STORAGEPAGE") != null) {
                storagePage = (Double.parseDouble(hashMap.get("STORAGEPAGE").toString()));
            }
            if (hashMap.get("STORAGENO") != null) {
                storageNo = (Double.parseDouble(hashMap.get("STORAGENO").toString()));
            }
            contractSub.setStorageNumber(storageNo);

        }else {
            storageSub.setStorageSubStatus("0"); //查询子项全部撤销sum所有直
            hashMap = storageSubService.selectTotalByOne(storageSub);
            if(hashMap !=null) {
                if (hashMap.get("STORAGEPAGE") != null) {
                    storagePage = (Double.parseDouble(hashMap.get("STORAGEPAGE").toString()));
                }
                if (hashMap.get("STORAGENO") != null) {
                    storageNo = (Double.parseDouble(hashMap.get("STORAGENO").toString()));
                }

                storage.setStorageStatus("0");
                contractSub.setStorageNumber(0.0);

            }
        }
        storage.setStorageWeight(storageNo);
        storage.setStoragePage(storagePage);
        update(storage);
        contractSubService.update(contractSub);
        return updateSubId;
    }


}
